package com.test.controller.leader;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.controller.Admininfo.ResData;
import com.test.entity.*;
import com.test.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class LeaderController {

    @Autowired
    private LeaderInfoService leaderInfoService;
    @Autowired
    private StaffInfoService staffInfoService;
    @Autowired
    private TaskInfoService taskInfoService;
    @Autowired
    private PlanInfoService planInfoService;
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private NoticeInfoService noticeInfoService;
    @Autowired
    private TxInfoService txInfoService;

//    进入部门领导页面
    @GetMapping("leader/index")
    public String getStaff(){
        return "leader/index";
    }
//    显示个人信息详情页
    @GetMapping("leader/leader_info/detail")
    public ModelAndView getLeaderDetail(HttpSession session){
        ModelAndView modelAndView = new ModelAndView("leader/personal");

        Map login =(Map)session.getAttribute("login");
        Integer id = (Integer)login.get("id");
        LeaderInfo leaderInfo = leaderInfoService.queryById(id);
        if (leaderInfo.getSex() == 1) {
            leaderInfo.setSexStr("男");
        } else {
            leaderInfo.setSexStr("女");
        }
        modelAndView.addObject("detail",leaderInfo);

        return modelAndView;
    }
//   进入部门员工页面
    @GetMapping("leader/staff_info")
    public String getStaffInfo(){
        return "leader/staff_info/list";
    }
    //    显示部门员工页面信息
    @GetMapping("leader/staff_info/queryList")
    @ResponseBody
    public ResData getStaffInfoQueryList(Integer page,StaffInfo staffInfo,HttpSession session){
        Map login =(Map)session.getAttribute("login");
        Integer id = (Integer)login.get("id");
        LeaderInfo leaderInfo = leaderInfoService.queryById(id);
        staffInfo.setDepartId(leaderInfo.getDepartId());

        return staffInfoService.queryByPage(staffInfo, PageRequest.of(page - 1, 10));
    }


//    进入任务管理页面
    @GetMapping("leader/task_info")
    public String getTaskInfo(){
        return "leader/task_info/list";
    }

    //    显示任务管理信息
    @GetMapping("leader/task_info/queryList")
    @ResponseBody
    public String getTaskInfoQueryList(Integer page, TaskInfo taskInfo) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(taskInfoService.queryByPage(taskInfo, PageRequest.of(page - 1, 10)));
    }

    //    跳转任务新增页面
    @GetMapping("leader/task_info/add")
    public ModelAndView getTaskInfoAdd() {
        ModelAndView modelAndView = new ModelAndView("leader/task_info/add_page");
        modelAndView.addObject("staffInfoList",staffInfoService.selectAll());
        return modelAndView;
    }

    //    提交任务新增
    @PostMapping("leader/task_info/add_submit")
    @ResponseBody
    public ResData postTaskInfoAddSubmit(TaskInfo taskInfo,HttpSession session) {
        ResData resData = new ResData();
        Map login =(Map)session.getAttribute("login");
        Integer id = (Integer)login.get("id");
        taskInfo.setLeaderId(id);

        int insert = taskInfoService.insert(taskInfo);
        if (insert>0) {
            resData.setCode(200);
            resData.setMsg("添加成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("添加失败");
        return resData;
    }

    //    跳转任务修改页面
    @GetMapping("leader/task_info/update")
    public ModelAndView getTaskInfoUpdate(Integer id) {
        ModelAndView modelAndView = new ModelAndView("leader/task_info/update_page");
        modelAndView.addObject("data", taskInfoService.queryById(id));
        modelAndView.addObject("staffInfoList",staffInfoService.selectAll());
        return modelAndView;
    }

    //    提交任务修改页面信息
    @PostMapping("leader/task_info/update_submit")
    @ResponseBody
    public ResData postTaskInfoUpdateSubmit(Integer id,TaskInfo taskInfo) {
        taskInfo.setId(id);
        System.out.println(taskInfo);
        ResData resData = new ResData();
        TaskInfo update = taskInfoService.update(taskInfo);
        if (update.getId() != null || !update.getId().equals("")) {
            resData.setCode(200);
            resData.setMsg("修改成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("修改失败");
        return resData;
    }

    //    删除任务信息
    @GetMapping("leader/task_info/del")
    @ResponseBody
    public ResData getTaskInfoDelete(Integer id) {
        ResData resData = new ResData();
        boolean b = taskInfoService.deleteById(id);
        if (b) {
            resData.setCode(1);
            resData.setMsg("删除成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("删除失败");
        return resData;
    }


//    员工计划页面
    @GetMapping("leader/plan_info")
    public String getPlanInfo(){
        return "leader/plan_info/list";
    }
//    显示员工计划信息
    @GetMapping("leader/plan_info/queryList")
    @ResponseBody
    public ResData getPlanInfoQueryList(Integer page, PlanInfo planInfo){
        return planInfoService.queryByPage(planInfo,PageRequest.of(page-1,10));
    }


    //    文件信息页面
    @GetMapping("leader/file_info")
    public String getFile_info(){
        return "leader/file_info/list";
    }
//    显示文件信息
    @GetMapping("leader/file_info/queryList")
    @ResponseBody
    public ResData getFileInfoQueryList(Integer page,FileInfo fileInfo){
        return fileInfoService.queryByPage(fileInfo,PageRequest.of(page-1,10));
    }


//    系统公告页面
    @GetMapping("leader/notice_info")
    public String getNoticeInfo(){
        return "leader/notice_info/list";
    }
//    显示系统公告信息
    @GetMapping("leader/notice_info/queryList")
    @ResponseBody
    public ResData getNoticeInfoQueryList(Integer page,NoticeInfo noticeInfo){
        return noticeInfoService.queryByPage(noticeInfo,PageRequest.of(page-1,10));
    }

    //通讯录页面
    @GetMapping("leader/tx_info")
    public String getTxInfo(){
        return "leader/tx_info/list";
    }

    @GetMapping("leader/tx_info/queryList")
    @ResponseBody
    public ResData getTxInfoQueryList(Integer page,TxInfo txInfo){
        return txInfoService.queryByPage(txInfo,PageRequest.of(page-1,10));
    }

}
